package com.yhyvr.api.service.sms;

import com.lz.kit.LogKit;
import com.lz.mybatis.jdbc.auto.dao.BoDictionaryMapper;
import com.lz.mybatis.jdbc.auto.dao.BoProxyMapper;
import com.lz.mybatis.jdbc.auto.model.BoDictionaryExample;
import com.lz.mybatis.jdbc.auto.model.BoProxy;
import com.lz.mybatis.jdbc.auto.model.BoProxyExample;
import com.yhyvr.pojo.req.api.ApiReqData;
import com.yhyvr.pojo.req.api.sms.SmsReq;
import com.yhyvr.pojo.resp.api.ApiRespData;
import com.yhyvr.pojo.resp.api.ApiResultCode;
import com.yhyvr.pojo.resp.api.sms.SmsResp;
import com.yhyvr.service.SmsService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 
* @ClassName: SmsReqService 
* @Description: 短信验证服务
* @author zhangqin
* @date 2016年4月20日 下午4:54:20 
*
 */
@Service
public class SmsReqService {
	@Resource
	private SmsService smsService;
	@Resource
	private BoProxyMapper boProxyMapper;
	@Resource
	private BoDictionaryMapper boDictionaryMapper;
	public ApiRespData<SmsResp> getCode(ApiReqData<SmsReq> apiReqData){
			try{
			SmsReq smsReq = apiReqData.getBody();
			if(StringUtils.isBlank(smsReq.getBusinessUri())) {
				//如果是空的不错处理
			}else{
				// 拦截器校验业务是否需要认证
				BoDictionaryExample boDictionaryExample = new BoDictionaryExample();
				boDictionaryExample.createCriteria().andValueEqualTo(smsReq.getBusinessUri()).andCodeEqualTo("apiPhoneCodeGetValidate");
				if(boDictionaryMapper.countByExample(boDictionaryExample)>0){
					int conut= 0;
					if("proxy".equals(smsReq.getSource())) {
						BoProxyExample boProxyExample = new BoProxyExample();
						boProxyExample.createCriteria().andBoProxyPhoneEqualTo(smsReq.getPhone().trim());
						conut = boProxyMapper.countByExample(boProxyExample);
					}
					if(conut==0){
						if(smsReq.getValidateType()==0){
							return ApiRespData.buildFail(ApiResultCode.PHNOENOTFUND);
						}else{
							//不做处理
						}

					}else{
						if(smsReq.getValidateType()==0){
							//不做处理
						}else{
							return ApiRespData.buildFail(ApiResultCode.PHNOEISUSE);
						}
					}
				}else{
					//不作处理
				}
			}
				String code = smsService.getValidateCode(smsReq.getPhone());
				SmsResp resp = new SmsResp();
				resp.setCode(code);
				return ApiRespData.buildSucc(resp);
			}catch(Exception e){
				LogKit.error(e.getMessage(),e);
				return ApiRespData.buildFail(ApiResultCode.FAIL,e.getMessage());
			}

	}


}
